<%@ page contentType="text/html;charset=utf-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
	<head>
		<title>宿舍批量更新</title>
		<link rel="stylesheet"
			href="<%=request.getContextPath()%>/css/common.css" type="text/css" />
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/dwr/interface/apartmentViewAction.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/dwr/interface/dormitoryAllocateAction.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/dwr/interface/buildingsMgrAction.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/dwr/engine.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/dwr/util.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/js/jquery-latest.pack.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/js/noConflictOfJquery.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/js/forCSS.js'></script>
		<script type="text/javascript">
		    var gaptInfo=""; //这个gaptInfo是全局变量 
	        var totalcount = -1; //查询到的总行数
	        var page;   //dwr分页的page对象，注意在页面中是一个Json对象
	        var dormCache ={};
	        var apartmentid;
	        var dormallocationid; // 宿舍批量更新的预分配id
	        var dormitoryIdList = new   Array();


		 //载入系统公寓信息
		function setApartList() {
		 apartmentViewAction.getAllApartInfo(mycallback);
		}
		var mycallback = function setList(aptValue)//回调函数
		{
			gaptInfo =aptValue;
			//alert(aptValue[0].apartmentfloornum);
			var s = $("aptName");
			 	DWRUtil.removeAllOptions(s);  
				s.options[0] = new Option("请选择公寓",'0');
			for ( var i = 1; i < aptValue.length+1; i++) {
				var opt = new Option(aptValue[i-1].apartmentname,
						aptValue[i-1].apartmentid);
				s.options[i] = opt;
			}
		}

		//载入预分配allcationinfo的内容
		function setAllocationInfo(){
			return dormitoryAllocateAction.getAllAllocationInfo(aicallback);
		}
		var aicallback = function aicall_back(aiList){
			var sp = $("updateassign");
			DWRUtil.removeAllOptions(sp);  
			sp.options[0] = new Option("预分配值",'0');
			for ( var i = 1; i < aiList.length+1; i++) {
				var opt = new Option(aiList[i-1].allocationname,
						aiList[i-1].allocationid);
				sp.options[i] = opt;
			}
		}
	
		function selectChange(){
				apartmentid = $("aptName").options[$("aptName").selectedIndex].value; //给apartmentid赋值
				dormallocationid = $("updateassign").options[$("updateassign").selectedIndex].value; //给dormallocationid赋值
		}
				
				
	//查询总行数
	function searchTotalCount(){
		if(apartmentid==undefined||apartmentid==0){
			alert("请选择公寓！");
			return;
		}
		if($('pageTable').style.visibility == 'visible'){
		$('pageTable').style.visibility = 'hidden';
		}
		if($('MyTable').style.visibility == 'visible'){
			$('MyTable').style.visibility = 'hidden';
		}
		if($('updatediv').style.visibility == 'visible'){
			$('updatediv').style.visibility = 'hidden';
		}
		if($('selectTable').style.visibility == 'visible'){
			$('selectTable').style.visibility = 'hidden';
		}
		buildingsMgrAction.getApartmentDormTotalRowsByID(apartmentid,tcbck); //查询宿舍
	}
	
	var tcbck = function tc_bck(bckcount){
	totalcount = bckcount; // 给总行数赋值
	buildingsMgrAction.ListAllDormListPerPageByApartmentID(1,totalcount,apartmentid,renderdormcallback);
	}
	
	var renderdormcallback  = function renderdormcb(dormlst){
		if(dormlst==null){
		alert("查询到的宿舍列表为空！");
		return;
		}
		if($('MyTable').style.visibility == 'hidden'){
			$('MyTable').style.visibility = 'visible';
		}
		if($('selectTable').style.visibility == 'hidden'){
			$('selectTable').style.visibility = 'visible';
		}
		if($('pageTable').style.visibility == 'hidden'){
		$('pageTable').style.visibility = 'visible';
		}
		if($('updatediv').style.visibility == 'hidden'){
			$('updatediv').style.visibility = 'visible';
		}
		
				DWRUtil.removeAllRows('ttb', { filter:function(tr) {
				   return (tr.id != "pattern");
					    }});
				var dom,id;
				//1,2,1,-1,101  ,1号楼 1层101  房间,0,0,6,900,-1,1,101001,6,1,6,信息女
				for (var i =0; i < dormlst.length ; i++) {
					  dom = dormlst[i];
					  id = dom[0];
					  //alert(id);
				  dwr.util.cloneNode("pattern", { idSuffix:id });
				  dwr.util.setValue("tableid"+id, dom[0]);//隐藏项
				  dwr.util.setValue("tablename"+id, dom[4]);
				  dwr.util.setValue("tabledisplayname"+id, dom[5]);
				  if(dom[6]==1){
				   dwr.util.setValue("tablesex"+id,'男');
				   }else{
				   dwr.util.setValue("tablesex"+id,'女');
				   }
				   dwr.util.setValue("tabletelephone"+id,dom[7]);
				   dwr.util.setValue("tableroomtype"+id,dom[8]);
				   dwr.util.setValue("tableroomfee"+id,dom[9]); //隐藏项
				   dwr.util.setValue("tablefunction"+id,dom[10]);//隐藏项
				   dwr.util.setValue("tableapartmentid"+id,dom[2]); //隐藏项
				   dwr.util.setValue("tableinamount"+id,dom[15]+'/'+dom[8]);
				   //dwr.util.setValue("tableavialamount"+id,dom[8]-dom[15]); //可住人数
				   dwr.util.setValue("tableprevalue"+id,dom[16]);
				   dwr.util.setValue("cbx" + id, dom[0]);
				   $("pattern" + id).style.display= "";
				   dormCache[id] = dom;
				}
				var Ptr= document.getElementById('MyTable').getElementsByTagName('tr'); 
				for (i=1;i<Ptr.length+1;i++) {
				    Ptr[i-1].className = (i%2>0)?"row_odd":"row_even";
				} 
			setPageInfo();
			tbCSS();
			}
	
	
	function setPageInfo(){
		$('pageTd').innerHTML = '';
		buildingsMgrAction.getPage({
			callback:function(data){
				page = data;
				//显示 当前页/总页数
				if(page.totalPage != 0){
					var pageSpan = document.createElement('span');
					//pageSpan.innerHTML = page.currentPage + '/' + page.totalPage;
					pageSpan.innerHTML = '共 '+totalcount+' 条 '+'   第 '+page.currentPage+' 页    共'+page.totalPage+' 页 ';
					$('pageTd').appendChild(pageSpan);
					
					var aFirst = document.createElement('a');
					aFirst.setAttribute('href','javascript:getFirstData()');
					aFirst.innerHTML = '首页';
					$('pageTd').appendChild(aFirst);
					var aPrevious = document.createElement('a');
					aPrevious.setAttribute('href','javascript:getPreviousData()');
					aPrevious.innerHTML = '上一页 ';
					$('pageTd').appendChild(aPrevious);
					var aNext = document.createElement('a');
					aNext.setAttribute('href','javascript:getNextData()');
					aNext.innerHTML = '下一页 ';
					$('pageTd').appendChild(aNext);
					var aLast = document.createElement('a');
					aLast.setAttribute('href','javascript:getLastData()');
					aLast.innerHTML = '尾页';
					$('pageTd').appendChild(aLast);
				}
				
			}
		});
	$('loading').style.display = 'none';
	judgeCheckboxInNewPage();
	$('checkbox_62').checked = false;
}

function getFirstData(){
	page.currentPage = 1;
	getDataPerPage();
}

function getPreviousData(){
		if(page.currentPage>1){
			page.currentPage--;
		}
		getDataPerPage();
	}
	
	function getNextData(){
		if(page.currentPage<page.totalPage){
			page.currentPage++;
		}
		getDataPerPage();
	}

function getLastData(){
	page.currentPage = page.totalPage;
	getDataPerPage();
}



function getDataPerPage(){
	$('loading').style.display = 'block';
	buildingsMgrAction.ListAllDormListPerPageByApartmentID(page.currentPage,totalcount,apartmentid,renderdormcallback)
}	

//批量更新宿舍
function updatebatchdorm(){
	if(dormallocationid==""){
		alert("请选择标记值!");
		return;
		}
	if(dormitoryIdList==""&&($('checkbox_63').checked == false)){
		alert("请选择宿舍!");
		return;
		}
	if(dormitoryIdList==""&&($('checkbox_63').checked == true)){
				/**
				*通过apartmentid和一条sql语句去批量更新所有宿舍
				*/
			if(apartmentid==""||apartmentid==undefined){
				alert("请选择要更新的宿舍!");
				return;
			}
			if(dormallocationid==""){
				alert("请选择标记值!");
				return;
				}
			buildingsMgrAction.updateAApartmentDormPreID(apartmentid,dormallocationid,aapartmentprecallback);
		}else{
	//alert(dormitoryIdList);
	buildingsMgrAction.updateBatchDormitoryPreassignInfo(dormitoryIdList+"",dormallocationid,updatecbck);
	//使所有已勾选的checkbox不勾选上
	var  a=document.getElementsByTagName("input");  
	for(var i=0;i<a.length;i++){  
		    if(a[i].type=='checkbox' && a[i].id != "checkbox_62" && a[i].id != "checkbox62") { 
		    	if(a[i].checked == true){
		       		a[i].checked = false;
		       		checkCheckboxInDormitoryIdList(a[i]);
		       	}
		    }
	}
	}
}
var updatecbck = function update_cbck(status){
	alert(status);
	searchTotalCount();
}

//当页全选
function selectCurrentPage(){
	var   a=document.getElementsByTagName("input");  
	if($('checkbox_62').checked == true){
	 	for(var i=0;i<a.length;i++){  
	       if(a[i].type=='checkbox' && a[i].id != "checkbox_62" && a[i].id != "checkbox_63") { 
	       		a[i].checked = true ;
	       		checkCheckboxInDormitoryIdList(a[i]);
	       	}
	    }
    }
    else if($('checkbox_62').checked == false){
	    for(var i=0;i<a.length;i++){  
		    if(a[i].type=='checkbox' && a[i].id != "checkbox_62" && a[i].id != "checkbox_63") { 
		       	a[i].checked = false;
		       	checkCheckboxInDormitoryIdList(a[i]);
		    }
		}
    }
}

//所有页全选
function selectAllPage(){
	//selectCurrentPage();//让用户感觉全部选中了.....其实通过sql去更新所有数据的
	var   aa=document.getElementsByTagName("input");  
	if($('checkbox_63').checked == true){
	 	for(var i=0;i<aa.length;i++){  
	       if(aa[i].type=='checkbox' && aa[i].id != "checkbox_62"&& aa[i].id != "checkbox_63" ) { 
	       		aa[i].checked = true ;
	       	}
	    }
    }
    else if($('checkbox_63').checked == false){
	    for(var i=0;i<aa.length;i++){  
		    if(aa[i].type=='checkbox' && aa[i].id != "checkbox_62"&& aa[i].id != "checkbox_63" ) { 
		       	aa[i].checked = false;
		    }
		}
    }
}
var aapartmentprecallback = function aapartmentprecall_back(data){
alert(data);
}

function checkDormitoryIdList(str){
		for(var a=0 ;a<dormitoryIdList.length;a++){
			if(str == dormitoryIdList[a]){
				return a;
			}else continue;
		}
		return -1;
	}
	function checkCheckboxInDormitoryIdList(obj){
		var p = checkDormitoryIdList(obj.id.substr(3));
		if(p == -1 && obj.checked == true){
			dormitoryIdList[dormitoryIdList.length] = obj.id.substr(3);
		}
		if(p != -1 && obj.checked == false){
			dormitoryIdList.splice(p,1);
		}
	}
	//dwr分页中，当进入到新的页面时判断当前页的checkbox是否曾经被选中，并存入了dormitoryIdList中
	function judgeCheckboxInNewPage(){
		var a=document.getElementsByTagName("input");  
        	for(var i=0;i<a.length;i++){  
              if(a[i].type=='checkbox'){  
                  if(checkDormitoryIdList(a[i].id.substr(3)) != -1){
                  	 a[i].checked = true;
                  }else a[i].checked = false;
              }
            }
	}


</script>
	</head>
	<body onload="javascript:setApartList(),setAllocationInfo()">
		<div id="page_container">
			<%@include file="mgrbanner.jsp"%>
			<h2>
				标记房间
			</h2>
			<div class="divground">
				<div id="search_title">
					<b class="rtop"><b class="r1"></b><b class="r2"></b><b
						class="r3"></b><b class="r4"></b> </b>
					<span><select class="common_select" name="aptName"
							id="aptName" style="width: 120"
							onchange="javascript:selectChange();"></select> </span>
					<span><input class="common_button" type="button" value="提交"
							onclick="javascript:searchTotalCount();" /> </span>
					<span><input type="checkbox" name="checkbox_62"
							id="checkbox_62" onclick="selectCurrentPage()" />当页全选</span>
					<span style="display:none"><input type="checkbox" name="checkbox_63"
							id="checkbox_63" onclick="selectAllPage()" />所有页全选</span>
					<b class="rbottom"><b class="r4"></b><b class="r3"></b><b
						class="r2"></b><b class="r1"></b> </b>
				</div>
				<table id="selectTable" style="visibility: hidden;">
					<tr>

						<td style="visibility: hidden;">
							<input type="checkbox" name="checkbox62" id="checkbox62"
								value="checkbox" />
							所有结果全选
						</td>
					</tr>
				</table>
				<!-- 隐藏的项目开始 -->
				<div  id="tableid" style="display: none"></div>
				<div  id="tableroomfee" style="display: none"></div>
				<div  id="tablefunction" style="display: none"></div>
				<div  id="tableapartmentid" style="display: none"></div>
				<!-- 隐藏的项目结束-->
				<table id="MyTable" class="table_common table_list_style"
					style="visibility: hidden;">
					<thead>
						<tr>
							<th width="6%">
								<div >
									选择
								</div>
							</th>
							<th width="24%" >
								<div >
									公寓名称
								</div>
							</th>
							<th width="24%" >
								<div >
									宿舍显示名
								</div>
							</th>
							<th width="9%" >
								<div >
									性别
								</div>
							</th>
							<th width="10%" >
								<div >
									房型
								</div>
							</th>
							<th width="14%" >
								<div >
									已住/总数
								</div>
							</th>
							<th width="14%" >
								<div >
									预分配值
								</div>
							</th>
						</tr>
					</thead>
					<tbody id="ttb">
						<tr id="pattern" style="display: none;">
							<td >
								<div >
									<input type="checkbox" id="cbx" name="cbx" value="checkbox"
										onclick="checkCheckboxInDormitoryIdList(this)" />
								</div>
							</td>
							<td >
								<div  id="tablename">
								</div>
							</td>
							<td >
								<div  id="tabledisplayname">
								</div>
							</td>
							<td >
								<div  id="tablesex">
								</div>
							</td>
							<td >
								<div  id="tableroomtype">
								</div>
							</td>
							<td >
								<div  id="tableinamount">
								</div>
							</td>
							<td >
								<div  id="tableprevalue">
								</div>
							</td>
						</tr>
					</tbody>
				</table>
				<table id="pageTable" 
					style="visibility: hidden; margin-top: 30px; font: 12px;"
					class="navigator">
					<tr>
						<td id="pageTd">
						</td>
					</tr>
				</table>
				<div id="loading"
					style="display: none; margin-top: 20px; text-align: center; color: red; font: 13px;">
					Loading...
				</div>
				<div id="updatediv" style="visibility: hidden;">
					<label>标记为:</label>
					<select class="common_select" name="updateassign" id="updateassign" style="width: 120"
						onchange="javascript:selectChange();">
					</select>
					<input class="common_button" type="submit" value="确定"
						onclick="javascript:updatebatchdorm();" />
				</div>
			</div>
		</div>
	</body>
</html>
